package com.ledim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.LedimAlbumBean;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.RoomAddPlayListResponse;
import com.ledim.ledimview.VideoAlbumsView;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;

/**
 * 剧集页
 */
public class EpisodesActivity extends LeDimBaseActivity {

    public static final String EPISODES = "episodes";
    public static final String ROOM_ID = "room";
    public static final String ALBUM = "album";
    public static final String MEDIAID = "mediaid";
    public static final String SUBMEDIAID = "submediaid";
    private ArrayList<LedimEpisodeBean> mSelectEpisodes = new ArrayList<LedimEpisodeBean>();
    private String mRoomId;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_episodes;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        int episodes = getIntent().getIntExtra(EPISODES, 0);
        mRoomId = getIntent().getStringExtra(ROOM_ID);
        boolean addPlay = !TextUtils.isEmpty(mRoomId);
        String mediaId = getIntent().getStringExtra(MEDIAID);
        String subMediaId = getIntent().getStringExtra(SUBMEDIAID);
        LedimAlbumBean mAlbum = (LedimAlbumBean) getIntent().getSerializableExtra(ALBUM);
        final VideoAlbumsView mAlbumsLayout = findViewByIdExt(R.id.albums_layout);
        final View mButtonLayout = findViewById(R.id.button_layout);
        final TextView addButton = findViewByIdExt(R.id.add_to_play_lists);
        final TextView chooseAllTv = findViewByIdExt(R.id.episodes_add_all);
        if (addPlay) {
            mButtonLayout.setVisibility(View.VISIBLE);
            addButton.setOnClickListener(this);
            chooseAllTv.setOnClickListener(this);
            chooseAllTv.setVisibility(View.VISIBLE);
            addButton.setClickable(false);
        }
        mAlbumsLayout.bindData(mAlbum, episodes, mediaId, subMediaId, addPlay, new EpisodesInterface() {
            @Override
            public void selectAllEpisodes(int arg1, int arg2, ArrayList<LedimEpisodeBean> list) {
                if (arg1 == 1) {
                    mSelectEpisodes.clear();
                    mSelectEpisodes.addAll(list);
                    addButton.setText("添加到放映单(" + mSelectEpisodes.size() + ")");
                    addButton.setClickable(true);
                    addButton.setBackgroundResource(R.drawable.rect_round_can_start_play_bg);
                } else if (arg1 == -1) {
                    addButton.setText("添加到放映单");
                    addButton.setClickable(false);
                    addButton.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                }
                //控制全选按钮，只要没有全选就显示全选
                if (arg2 == 1) {
                    chooseAllTv.setText("取消");
                } else {
                    chooseAllTv.setText("全选");
                }
            }

            @Override
            public void selectEpisodesPage(int arg1, int arg2) {
                //剧集tab的切换时
                if (arg1 == 0) {
                    chooseAllTv.setText("全选");
                    addButton.setText("添加到放映单");
                    addButton.setClickable(false);
                    addButton.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                } else if (arg1 == -1) {
                    chooseAllTv.setText("取消");
                    addButton.setText("添加到放映单(" + arg2 + ")");
                    addButton.setClickable(true);
                    addButton.setBackgroundResource(R.drawable.rect_round_can_start_play_bg);
                } else {
                    chooseAllTv.setText("全选");
                    addButton.setText("添加到放映单(" + arg1 + ")");
                    addButton.setClickable(true);
                    addButton.setBackgroundResource(R.drawable.rect_round_can_start_play_bg);
                }
            }

            @Override
            public void onClickEpisode(int playId) {
                Intent intent = new Intent();
                intent.putExtra(ConstantUtil.ID, playId);
                setResult(-1, intent);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_to_play_lists:
                LedimApiServiceUtils.addRoomPlayList(mRoomId, mSelectEpisodes, new LedimHttpObserver<RoomAddPlayListResponse>() {
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.toast("添加失败,请稍候重试");
                    }

                    @Override
                    public void onNext(RoomAddPlayListResponse response) {
                        super.onNext(response);
                        if (response != null && response.success) {
                            finish();
                            ToastUtil.toast("好了,大王");
                        } else {
                            if (response.code == 404) {
                                ToastUtil.toast("内容太高能,添加失败");
                            } else {
                                ToastUtil.toast("添加失败,请稍候重试");
                            }
                        }

                    }
                });
                break;
        }
    }

    public interface EpisodesInterface {

        void selectAllEpisodes(int arg1, int arg2, ArrayList<LedimEpisodeBean> list);

        void selectEpisodesPage(int arg1, int arg2);

        void onClickEpisode(int playId);
    }
}
